#!/usr/bin/env python
#coding:utf-8

import os
import math
import time
import functools

def metric(fn):
	@functools.wraps(fn)
	def decorator(*args, **kw):
		time_begin = time.time() * 1000	# second -> ms
		rtn = fn(*args, **kw)
		time_end = time.time() * 1000
		print('%s executed in %d ms' % (fn.__name__, time_end - time_begin))
		return rtn
	return decorator



# 测试
@metric
def fast(x, y):

	time.sleep(0.0012)
	return x + y;


@metric
def slow(x, y, z):
	time.sleep(0.1234)
	return x * y * z;

def log(text=''):
	def decorator(fn):
		@functools.wraps(fn)
		def decorator(*args, **kw):
			print text,"in"
			rtn = fn(*args, **kw)
			print text,"out"
			return rtn
		return decorator
	return decorator


@log
def f():
	print 'f'

@log('execute')
def f1():
	print 'f1'


if __name__ == "__main__":

	print('fast.name=%s' % (fast.__name__))

	f()